import { registFunction } from './plugins';

export function Element(widget, attribs) {
  this.l = this.r = this.t = this.b = 0; // 边框
  this.marginLeft = this.marginRight = this.marginTop = this.marginBottom = 0; //间距
  this.position = {};
  this.classMap = {};
  this.classList = [];
  this.children = [];
  this.attribs = attribs || {};
  this.styles = {};
  // 兼容处理
  this['0'] = this;
  this.style = {};
  if (!widget) {
    this.nodeName = 'body';
    this.position.x = 0;
    this.position.y = 0;
    this.attribs.id = 'body';
  } else if (BI.isWidget(widget)) {
    this.widget = widget;
    this.nodeName = widget.options.tagName;
    this.textBaseLine = widget.options.textBaseLine;
  } else if (BI.isString(widget)) {
    this.nodeName = widget;
  }
}

initElement(Element);
registFunction(Element);

function initElement(element) {
  element.prototype = {
    appendChild(child) {
      child.parent = this;
      if (this.children.push(child) !== 1) {
        var sibling = this.children[this.children.length - 2];
        sibling.next = child;
        child.prev = sibling;
        child.next = null;
      }
    },
    append(child) {
      child.parent = this;
      if (this.children.push(child) !== 1) {
        var sibling = this.children[this.children.length - 2];
        sibling.next = child;
        child.prev = sibling;
        child.next = null;
      }
    },
    getParent() {
      return this.parent;
    },
    getSiblings() {
      var parent = this.getParent();
      return parent ? parent.getChildren() : [this];
    },
    getChildren() {
      return this.children;
    },

    getBounds() {
      return {};
    },

    width() {

    },
    height() {

    }
  };
}
